home *** CD-ROM | disk | FTP | other *** search
/ All for Cell Phones: Sony Ericsson / Sony-Ericsson 2004.iso / Java / BugBug / bugbug.jar / bugbug / BugBug.class (.txt) < prev    next >
Encoding:
Java Class File  |  2001-07-25  |  8.5 KB  |  369 lines

  1. package bugbug;
  2.  
  3. import com.siemens.mp.game.Light;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.DataInputStream;
  7. import java.io.DataOutputStream;
  8. import java.io.IOException;
  9. import java.io.InputStream;
  10. import java.util.Hashtable;
  11. import java.util.Vector;
  12. import javax.microedition.io.Connector;
  13. import javax.microedition.io.HttpConnection;
  14. import javax.microedition.lcdui.Command;
  15. import javax.microedition.lcdui.CommandListener;
  16. import javax.microedition.lcdui.Display;
  17. import javax.microedition.lcdui.Displayable;
  18. import javax.microedition.lcdui.Image;
  19. import javax.microedition.lcdui.List;
  20. import javax.microedition.lcdui.TextBox;
  21. import javax.microedition.midlet.MIDlet;
  22. import javax.microedition.rms.RecordStore;
  23. import javax.microedition.rms.RecordStoreException;
  24.  
  25. public class BugBug extends MIDlet implements CommandListener {
  26.    static final String EXIT_COMMAND_LABEL = "Exit";
  27.    static final String START_COMMAND_LABEL = "Restart";
  28.    static final String OK_COMMAND_LABEL = "OK";
  29.    Display display;
  30.    YopparaiCanvas canvas;
  31.    Thread updateThread;
  32.    int mode = 0;
  33.    Command exitCommand;
  34.    TextBox tbox;
  35.    Vector testVector;
  36.    List ranker;
  37.    private String ConstantValue;
  38.    private String country;
  39.    // $FF: renamed from: I long
  40.    private long field_0;
  41.    // $FF: renamed from: J int
  42.    private static int field_1 = 100;
  43.    Vector ranking;
  44.  
  45.    public BugBug() {
  46.       Light.setLightOn();
  47.       this.canvas = new YopparaiCanvas(this);
  48.       this.display = Display.getDisplay(this);
  49.       this.exitCommand = new Command("Exit", 1, 0);
  50.    }
  51.  
  52.    protected void destroyApp(boolean var1) {
  53.       this.display.setCurrent((Displayable)null);
  54.       ((MIDlet)this).notifyDestroyed();
  55.    }
  56.  
  57.    protected void pauseApp() {
  58.       ((MIDlet)this).notifyPaused();
  59.    }
  60.  
  61.    protected void startApp() {
  62.       this.game();
  63.       long var1 = System.currentTimeMillis() / 1000L;
  64.       int var3 = Rand.rand();
  65.       String var4 = var1 + "." + var3;
  66.       String var5 = "http://s.si.klab.org/s/s?t=1&v=1&q=" + String.valueOf(this.getAccessSequence()) + "&id=" + var4 + "&c=0&player=nobody&country=somewhere&score=0";
  67.       this.field_0 = 0L;
  68.    }
  69.  
  70.    public void inputData() {
  71.       switch (this.mode) {
  72.          case 1:
  73.             this.tbox = new TextBox("your name", "", 256, 0);
  74.             break;
  75.          case 2:
  76.             this.tbox = new TextBox("your country", "", 12, 0);
  77.          case 3:
  78.          default:
  79.             break;
  80.          case 4:
  81.             this.tbox = new TextBox("Please!", "Please enter your name!", 256, 0);
  82.             break;
  83.          case 5:
  84.             this.tbox = new TextBox("Please!", "Please enter your country!", 256, 0);
  85.       }
  86.  
  87.       this.tbox.setCommandListener(this);
  88.       this.display.setCurrent(this.tbox);
  89.    }
  90.  
  91.    public void game() {
  92.       Command var1 = new Command("Restart", 1, 0);
  93.       this.canvas.addCommand(var1);
  94.       this.canvas.addCommand(this.exitCommand);
  95.       this.canvas.setCommandListener(this);
  96.       this.display.setCurrent(this.canvas);
  97.       this.updateThread = new Thread(this.canvas);
  98.       this.updateThread.start();
  99.    }
  100.  
  101.    public void commandAction(Command var1, Displayable var2) {
  102.       String var3 = var1.getLabel();
  103.       String var4;
  104.       if (this.tbox == null) {
  105.          var4 = "";
  106.       } else {
  107.          var4 = this.tbox.getString().trim();
  108.       }
  109.  
  110.       switch (this.mode) {
  111.          case 0:
  112.             if (var3 == "Exit") {
  113.                this.display.setCurrent((Displayable)null);
  114.                this.updateThread = null;
  115.                this.canvas = null;
  116.                this.destroyApp(true);
  117.             } else if (var3 == "Restart") {
  118.                this.canvas.init();
  119.             }
  120.             break;
  121.          case 1:
  122.             if (var4.length() == 0) {
  123.                this.mode = 4;
  124.                this.inputData();
  125.             } else {
  126.                this.ConstantValue = this.tbox.getString().trim();
  127.                this.mode = 2;
  128.                this.inputData();
  129.             }
  130.             break;
  131.          case 2:
  132.             if (var4.length() == 0) {
  133.                this.mode = 5;
  134.                this.inputData();
  135.             } else {
  136.                this.country = this.tbox.getString().trim();
  137.                this.mode = 0;
  138.                this.tbox = null;
  139.                System.gc();
  140.                this.display.setCurrent(this.canvas);
  141.             }
  142.             break;
  143.          case 3:
  144.             this.mode = 0;
  145.             this.display.setCurrent(this.canvas);
  146.             this.canvas.init();
  147.             break;
  148.          case 4:
  149.             this.mode = 1;
  150.             this.inputData();
  151.             break;
  152.          case 5:
  153.             this.mode = 2;
  154.             this.inputData();
  155.       }
  156.  
  157.    }
  158.  
  159.    public void setMode(int var1) {
  160.       this.mode = var1;
  161.    }
  162.  
  163.    public String getPlayerName() {
  164.       return this.ConstantValue;
  165.    }
  166.  
  167.    public String getCountry() {
  168.       return this.country;
  169.    }
  170.  
  171.    public void checkInetHighscore() {
  172.       long var1 = System.currentTimeMillis() / 1000L;
  173.       int var3 = Rand.rand();
  174.       String var4 = var1 + "." + var3;
  175.       (new StringBuffer()).append("http://s.si.klab.org/s/s?t=1&v=1&q=").append(String.valueOf(this.getAccessSequence())).append("&id=").append(String.valueOf(var4)).append("&c=0&player=").append(this.ConstantValue).append("&country=").append(this.country).append("&score=").append(this.field_0).toString();
  176.       this.mode = 3;
  177.       if (field_1 > 0 && field_1 <= 10) {
  178.          this.display.setCurrent(this.ranker);
  179.       } else if (field_1 > 10 && field_1 <= 100) {
  180.          TextBox var7 = new TextBox("Internet Ranking", "Your are the " + field_1 + "th.", 256, 0);
  181.          ((Displayable)var7).setCommandListener(this);
  182.          this.display.setCurrent(var7);
  183.       } else if (field_1 == 0) {
  184.          TextBox var6 = new TextBox("Internet Ranking", "Your are not in TOP100", 256, 0);
  185.          ((Displayable)var6).setCommandListener(this);
  186.          this.display.setCurrent(var6);
  187.       }
  188.  
  189.    }
  190.  
  191.    public void showInetHighscore() {
  192.       if (this.ranker == null) {
  193.          long var1 = System.currentTimeMillis() / 1000L;
  194.          int var3 = Rand.rand();
  195.          String var4 = var1 + "." + var3;
  196.          String var5 = "http://s.si.klab.org/s/s?t=1&v=1&q=" + String.valueOf(this.getAccessSequence()) + "&id=" + var4 + "&c=0&player=nobody&country=somewhere&score=0";
  197.       }
  198.  
  199.       this.mode = 3;
  200.       this.display.setCurrent(this.ranker);
  201.    }
  202.  
  203.    public void getRanking(String var1) {
  204.       try {
  205.          this.getViaHttpConnection(var1);
  206.       } catch (Exception var4) {
  207.       }
  208.  
  209.       this.ranker = new List("Internet Ranking", 3);
  210.       this.ranker.setCommandListener(this);
  211.  
  212.       for(int var2 = 0; var2 < this.ranking.size(); ++var2) {
  213.          Hashtable var3 = (Hashtable)this.ranking.elementAt(var2);
  214.          this.ranker.append(var2 + 1 + ": " + (String)var3.get("name") + " " + (String)var3.get("country") + " " + (Integer)var3.get("score"), (Image)null);
  215.       }
  216.  
  217.    }
  218.  
  219.    public long getAccessSequence() {
  220.       long var1 = -1L;
  221.  
  222.       try {
  223.          RecordStore var3 = RecordStore.openRecordStore("A", true);
  224.          if (var3.getNumRecords() > 0) {
  225.             byte[] var4 = var3.getRecord(1);
  226.             ByteArrayInputStream var5 = new ByteArrayInputStream(var4);
  227.             DataInputStream var6 = new DataInputStream(var5);
  228.             var1 = var6.readLong();
  229.             var6.close();
  230.             var5.close();
  231.          } else {
  232.             var1 = -1L;
  233.          }
  234.  
  235.          var3.closeRecordStore();
  236.       } catch (RecordStoreException var10) {
  237.          System.out.println(var10);
  238.       } catch (IllegalStateException var11) {
  239.          System.out.println(var11);
  240.       } catch (IOException var12) {
  241.          System.out.println(var12);
  242.       }
  243.  
  244.       ++var1;
  245.  
  246.       try {
  247.          RecordStore var14 = RecordStore.openRecordStore("A", true);
  248.          ByteArrayOutputStream var15 = new ByteArrayOutputStream();
  249.          DataOutputStream var16 = new DataOutputStream(var15);
  250.          var16.writeLong(var1);
  251.          byte[] var17 = var15.toByteArray();
  252.          if (var14.getNumRecords() == 0) {
  253.             var14.addRecord(var17, 0, var17.length);
  254.          } else {
  255.             var14.setRecord(1, var17, 0, var17.length);
  256.          }
  257.  
  258.          var16.close();
  259.          var15.close();
  260.          var14.closeRecordStore();
  261.       } catch (RecordStoreException var7) {
  262.          System.out.println(var7);
  263.       } catch (IllegalStateException var8) {
  264.          System.out.println(var8);
  265.       } catch (IOException var9) {
  266.          System.out.println(var9);
  267.       }
  268.  
  269.       return var1;
  270.    }
  271.  
  272.    public void saveHighscore() {
  273.       try {
  274.          RecordStore var1 = RecordStore.openRecordStore("A", true);
  275.          ByteArrayOutputStream var2 = new ByteArrayOutputStream();
  276.          DataOutputStream var3 = new DataOutputStream(var2);
  277.          var3.writeLong(this.field_0);
  278.          byte[] var4 = var2.toByteArray();
  279.          if (var1.getNumRecords() == 1) {
  280.             var1.addRecord(var4, 0, var4.length);
  281.          } else {
  282.             var1.setRecord(2, var4, 0, var4.length);
  283.          }
  284.  
  285.          var3.close();
  286.          var2.close();
  287.          var1.closeRecordStore();
  288.       } catch (RecordStoreException var5) {
  289.       } catch (IllegalStateException var6) {
  290.       } catch (IOException var7) {
  291.       }
  292.  
  293.    }
  294.  
  295.    public void setLocalHighscore(int var1) {
  296.       this.field_0 = (long)var1;
  297.    }
  298.  
  299.    public long getLocalHighscore() {
  300.       try {
  301.          RecordStore var1 = RecordStore.openRecordStore("A", false);
  302.          if (var1.getNumRecords() > 1) {
  303.             byte[] var2 = var1.getRecord(2);
  304.             ByteArrayInputStream var3 = new ByteArrayInputStream(var2);
  305.             DataInputStream var4 = new DataInputStream(var3);
  306.             this.field_0 = var4.readLong();
  307.             var4.close();
  308.             var3.close();
  309.          } else {
  310.             this.field_0 = 0L;
  311.          }
  312.  
  313.          var1.closeRecordStore();
  314.       } catch (RecordStoreException var5) {
  315.       } catch (IllegalStateException var6) {
  316.       } catch (IOException var7) {
  317.       }
  318.  
  319.       return this.field_0;
  320.    }
  321.  
  322.    public void getViaHttpConnection(String var1) throws IOException {
  323.       HttpConnection var2 = null;
  324.       InputStream var3 = null;
  325.       this.ranking = new Vector(10);
  326.  
  327.       try {
  328.          var2 = (HttpConnection)Connector.open(var1);
  329.          var3 = var2.openInputStream();
  330.          String var4 = var2.getType();
  331.          int var5 = (int)var2.getLength();
  332.          byte[] var6 = new byte[var5];
  333.          var3.read(var6);
  334.          ByteArrayInputStream var7 = new ByteArrayInputStream(var6);
  335.          DataInputStream var8 = new DataInputStream(var7);
  336.          if (var5 > 0) {
  337.             short var9 = var8.readShort();
  338.             int var10 = var8.readByte() & 255;
  339.             field_1 = var8.readByte() & 255;
  340.  
  341.             for(int var11 = 0; var11 < 10; ++var11) {
  342.                int var12 = var8.readByte() & 255;
  343.                int var13 = var8.readByte() & 255;
  344.                byte[] var14 = new byte[var12];
  345.                var8.read(var14, 0, var12);
  346.                byte[] var15 = new byte[var13];
  347.                var8.read(var15, 0, var13);
  348.                int var16 = var8.readInt();
  349.                Hashtable var17 = new Hashtable();
  350.                var17.put("score", new Integer(var16));
  351.                var17.put("name", new String(var14));
  352.                var17.put("country", new String(var15));
  353.                this.ranking.addElement(var17);
  354.             }
  355.          }
  356.       } finally {
  357.          if (var3 != null) {
  358.             var3.close();
  359.          }
  360.  
  361.          if (var2 != null) {
  362.             var2.close();
  363.          }
  364.  
  365.       }
  366.  
  367.    }
  368. }
  369.